package com.test.expandablegridview;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Author: dengdayi
 * Date: 2015/9/17 16:13
 * Description:
 */
public class MyAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<GroupModel> groupModels;
    private LayoutInflater layoutInflater;
    private List<List<GroupModel>> listList;
    final ExpandableListView expandableListView;
    /**
     * 每行中的索引值
     */
    private Integer columnsPosition = 0;
    /**
     * 记录上一次条目点击中的item
     */
    private int oldColumnsPosition=-1;

    public MyAdapter(Context mContext, List<List<GroupModel>> listList, ExpandableListView expandableListView) {
        this.expandableListView = expandableListView;
        this.listList = listList;
        this.mContext = mContext;
        if (layoutInflater == null) {
            layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    }

    @Override
    public int getGroupCount() {
        return listList.size();
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return listList.get(groupPosition).get(columnsPosition).getChildModels().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupModels.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return listList.get(groupPosition).get(columnsPosition).getChildModels().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    int i = 0;

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        i++;

        ViewGroupHolder viewGroupHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_group, null);
            viewGroupHolder = new ViewGroupHolder();
            viewGroupHolder.gridViewItemLeft = (LinearLayout) convertView.findViewById(R.id.gvi_groupLeft);
            viewGroupHolder.gridViewItemCenter = (LinearLayout) convertView.findViewById(R.id.gvi_groupCenter);
            viewGroupHolder.gridViewItemRight = (LinearLayout) convertView.findViewById(R.id.gvi_groupRight);

            viewGroupHolder.textViewLeft = (TextView) viewGroupHolder.gridViewItemLeft.findViewById(R.id.tv_group_text);
            viewGroupHolder.textViewCenter = (TextView) viewGroupHolder.gridViewItemCenter.findViewById(R.id.tv_group_text);
            viewGroupHolder.textViewRight = (TextView) viewGroupHolder.gridViewItemRight.findViewById(R.id.tv_group_text);


            convertView.setTag(viewGroupHolder);
        } else {
            viewGroupHolder = (ViewGroupHolder) convertView.getTag();
        }
        //设置数据
        List<GroupModel> models = listList.get(groupPosition);
        GroupModel groupModel0 = models.get(0);

        String s0 = groupModel0.getGroupName();
        if (!TextUtils.isEmpty(s0)) {
            viewGroupHolder.textViewLeft.setText(s0);
        }

        GroupModel groupModel1 = models.get(1);
        String s1 = groupModel1.getGroupName();
        if (!TextUtils.isEmpty(s1)) {
            viewGroupHolder.textViewCenter.setText(s1);
        }

        GroupModel groupModel2 = models.get(2);
        String s2 = groupModel2.getGroupName();
        if (!TextUtils.isEmpty(s2)) {
            viewGroupHolder.textViewRight.setText(s2);
        }
        //添加点击事件
        viewGroupHolder.gridViewItemLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                columnsPosition = 0;
                expandGroupToPosition(expandableListView, groupPosition,columnsPosition);//展开选中的条目
                notifyDataSetChanged();
            }
        });
        //添加点击事件
        viewGroupHolder.gridViewItemCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                columnsPosition = 1;
                expandGroupToPosition(expandableListView, groupPosition,columnsPosition);//展开选中的条目
                notifyDataSetChanged();
            }
        });
        //添加点击事件
        viewGroupHolder.gridViewItemRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                columnsPosition = 2;
                expandGroupToPosition(expandableListView, groupPosition,columnsPosition);//展开选中的条目
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewChildHolder viewChildHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_child, null);
            viewChildHolder = new ViewChildHolder();
            viewChildHolder.childTextView = (TextView) convertView.findViewById(R.id.tv_child_text);
            convertView.setTag(viewChildHolder);
        } else {
            viewChildHolder = (ViewChildHolder) convertView.getTag();
        }
        //填充数据
        String string = listList.get(groupPosition).get(columnsPosition).getChildModels().get(childPosition).getChildName();
        viewChildHolder.childTextView.setText(string);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class ViewGroupHolder {
        LinearLayout gridViewItemLeft;
        LinearLayout gridViewItemCenter;
        LinearLayout gridViewItemRight;

        TextView textViewLeft;
        TextView textViewCenter;
        TextView textViewRight;

    }

    private class ViewChildHolder {
        TextView childTextView;
    }

    public Integer getColumnsPosition() {
        return columnsPosition;
    }


    /**
     * 展开所选中的条目，
     * @param expandableListView
     * @param groupPosition
     */
    private void expandGroupToPosition(ExpandableListView expandableListView, Integer groupPosition,Integer columnsPosition) {
        //关闭其他条目
        for (int i = 0; i < listList.size(); i++) {
            if (i != groupPosition) {
                if (expandableListView.isGroupExpanded(i)) {
                    expandableListView.collapseGroup(i);
                }
            }
        }
        Log.i("dengdayi","isGroupExpanded:"+expandableListView.isGroupExpanded(groupPosition));
        //展开选中条目
        if(expandableListView.isGroupExpanded(groupPosition)){
            if(oldColumnsPosition==columnsPosition){
                expandableListView.collapseGroup(groupPosition);
            }
        }else {
            expandableListView.expandGroup(groupPosition);
        }
        oldColumnsPosition=columnsPosition;

    }
}
